from spidertools.utils.time_utils import get_current_date

from commonresources.spider_items.base_item import convert_dict
from commonresources.spider_items.zhejiang.items import ZheJiangZhengFuCaiGouWangItem
from commonresources.spiders.basespider import BaseSpider
from scrapy.http import Request, FormRequest
import time


class ZheJiangZhengFuCaiGouWangSpider(BaseSpider):
    """
        浙江政府采购网  http://zfcg.czt.zj.gov.cn/
    """
    name = 'ZheJiangZhengFuCaiGouWang'
    name_zh = '浙江政府采购网'
    province = "浙江"
    start_urls = ['http://zfcg.czt.zj.gov.cn/purchaseNotice/index.html']

    def __init__(self, full_dose=False):
        self.convert_dict = convert_dict
        super(ZheJiangZhengFuCaiGouWangSpider, self).__init__(full_dose)

    def parse(self, response):
        root_objs = response.xpath('//div[@class="gpoz-nav"]/div')
        for i in range(len(root_objs) // 2):
            construction_type_1 = root_objs[2 * i].xpath('./text()').extract_first()
            """
            print(root_objs[2 * i + 1].xpath('./@id').extract_first())
            print(type(root_objs))  # <class 'scrapy.selector.unified.SelectorList'>
            print(type(root_objs[2 * i + 1]))  # <class 'scrapy.selector.unified.Selector'>
            """
            second_objs = root_objs[2 * i + 1].xpath('./*')
            for j in range(len(second_objs) // 2):
                construction_type_2 = second_objs[2 * j].xpath('./text()').extract_first()
                for m in second_objs[2 * j + 1].xpath('./*'):
                    construction_type_3 = m.xpath('./a/text()').extract_first()
                    data_num = m.xpath('./@data-num').extract_first()
                    construction_type = "_".join([construction_type_1, construction_type_2, construction_type_3])
                    url = f"http://zfcgmanager.czt.zj.gov.cn/cms/api/cors/remote/results?" \
                          f"pageSize=15&pageNo=1&sourceAnnouncementType={data_num}&url=notice"
                    yield Request(
                        url=url,
                        dont_filter=True,
                        meta={
                            'construction_type': construction_type,
                            'need_break': False,
                            'page_count': -1,
                            'page': 1,
                            'data_num': data_num,
                        },
                        callback=self.handle_response,
                    )

    def handle_response(self, response):
        response_objs = response.json()
        if response.meta['page_count'] == -1:
            response.meta['page_count'] = int(response_objs['count']) // 15 + 1
        item_objs = response_objs['articles']
        for obj in item_objs:
            item = dict()
            t = time.localtime(int(obj['pubDate'][0:-3]))
            item['release_time'] = time.strftime("%Y-%m-%d", t)
            if not self.full_dose and item['release_time'] != get_current_date():
                response.meta['need_break'] = True
            else:
                item['construction_type'] = response.meta['construction_type']
                item['project_area'] = obj['districtName']
                notice_id = obj['url'].split('=')[-1]
                url = f'http://zfcgmanager.czt.zj.gov.cn/cms/api/cors/remote/results?noticeId={notice_id}&url=noticeDetail'
                item['origin_url'] = url
                item['announcement_title'] = obj['title']
                item_obj = ZheJiangZhengFuCaiGouWangItem()
                item['item'] = item_obj
                yield Request(url=item['origin_url'],
                              callback=self.parse_item,
                              meta=item,
                              dont_filter=True,
                              )

        if not response.meta['need_break']:
            page = response.meta['page']
            page_count = response.meta['page_count']
            if page < page_count:
                page += 1
                data_num = response.meta['data_num']
                url = f"http://zfcgmanager.czt.zj.gov.cn/cms/api/cors/remote/results?" \
                      f"pageSize=15&pageNo={page}&sourceAnnouncementType={data_num}&url=notice"
                yield Request(
                    url=url,
                    dont_filter=True,
                    meta={
                        'construction_type': response.meta['construction_type'],
                        'need_break': False,
                        'page_count': page_count,
                        'page': page,
                        'data_num': data_num,
                    },
                    callback=self.handle_response,
                )
